<?php

namespace App\Http\Controllers\Img;

use App\Http\Controllers\Controller;
use App\Models\News;
use Illuminate\Http\Request;
use Validator;


class ImgController extends Controller
{
    public function NewsCoverImg(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'cover_img' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        $temp = explode('.', $_FILES['cover_img']['name']);
        $extension = end($temp);
        if ((($_FILES['cover_img']['type'] == 'image/gif')
            || ($_FILES['cover_img']['type'] == 'image/jpeg')
            || ($_FILES['cover_img']['type'] == 'image/jpg')
            || ($_FILES['cover_img']['type'] == 'image/pjpeg')
            || ($_FILES['cover_img']['type'] == 'image/x-png')
            || ($_FILES['cover_img']['type'] == 'image/png'))
            && ($_FILES['cover_img']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['cover_img']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return redirect()->back();
            } else {
                $picname = 'cover_img' . '.' . $extension;
                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/news/';
                $url = './temp/' . $request->u_id . '/news/CoverImg/' . $picname;
                $third_dir = './temp/' . $request->u_id . '/news/CoverImg/';
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!is_dir($third_dir)) {
                    mkdir($third_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }
                move_uploaded_file($_FILES['cover_img']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => $url, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    public function NewsContentImgInsert(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'content_img' => 'required',
            'u_id' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        $temp = explode('.', $_FILES['content_img']['name']);
        $extension = end($temp);
        if ((($_FILES['content_img']['type'] == 'image/gif')
            || ($_FILES['content_img']['type'] == 'image/jpeg')
            || ($_FILES['content_img']['type'] == 'image/jpg')
            || ($_FILES['content_img']['type'] == 'image/pjpeg')
            || ($_FILES['content_img']['type'] == 'image/x-png')
            || ($_FILES['content_img']['type'] == 'image/png'))
            && ($_FILES['content_img']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['content_img']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return redirect()->back();
            } else {
                $id = News::max('id');
                ++$id;
                $totalimgs = glob('./temp/' . $request->u_id . '/news/ContentImg/' . '*');
                $FileNumber = count($totalimgs);
                $picname = $id . '_' . $FileNumber . '.' . $extension;

                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/news/';
                $url = './temp/' . $request->u_id . '/news/ContentImg/' . $picname;
                $third_dir = './temp/' . $request->u_id . '/news/ContentImg/';
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!is_dir($third_dir)) {
                    mkdir($third_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }

                move_uploaded_file($_FILES['content_img']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => $url, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    public function NewsContentImgUpdate(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'content_img' => 'required',
            'id' => 'required',
            'u_id' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        $temp = explode('.', $_FILES['content_img']['name']);
        $extension = end($temp);
        if ((($_FILES['content_img']['type'] == 'image/gif')
            || ($_FILES['content_img']['type'] == 'image/jpeg')
            || ($_FILES['content_img']['type'] == 'image/jpg')
            || ($_FILES['content_img']['type'] == 'image/pjpeg')
            || ($_FILES['content_img']['type'] == 'image/x-png')
            || ($_FILES['content_img']['type'] == 'image/png'))
            && ($_FILES['content_img']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['content_img']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return redirect()->back();
            } else {
                $totalimgs = glob('./temp/' . $request->u_id . '/news/ContentImg/' . '*');
                $FileNumber = count($totalimgs);
                $picname = $request->id . '_' . $FileNumber . '.' . $extension;

                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/news/';
                $url = './temp/' . $request->u_id . '/news/ContentImg/' . $picname;
                $third_dir = './temp/' . $request->u_id . '/news/ContentImg/';
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!is_dir($third_dir)) {
                    mkdir($third_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }

                move_uploaded_file($_FILES['content_img']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => './news/ContentImg/' . $picname, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    public function HonourCoverImg(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'cover_img' => 'required',
            'u_id' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        $temp = explode('.', $_FILES['cover_img']['name']);
        $extension = end($temp);

        if ((($_FILES['cover_img']['type'] == 'image/gif')
            || ($_FILES['cover_img']['type'] == 'image/jpeg')
            || ($_FILES['cover_img']['type'] == 'image/jpg')
            || ($_FILES['cover_img']['type'] == 'image/pjpeg')
            || ($_FILES['cover_img']['type'] == 'image/x-png')
            || ($_FILES['cover_img']['type'] == 'image/png'))
            && ($_FILES['cover_img']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['cover_img']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return json_encode($data);
            } else {
                $picname = 'cover_img' . '.' . $extension;

                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/honour/';
                $url = './temp/' . $request->u_id . '/honour/CoverImg/' . $picname;
                $third_dir = './temp/' . $request->u_id . '/honour/CoverImg/';
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!is_dir($third_dir)) {
                    mkdir($third_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }
                move_uploaded_file($_FILES['cover_img']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => './news/ContentImg/' . $picname, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    public function ProductCoverImg(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'cover_img' => 'required',
            'u_id' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $temp = explode('.', $_FILES['cover_img']['name']);
        $extension = end($temp);
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        if ((($_FILES['cover_img']['type'] == 'image/gif')
            || ($_FILES['cover_img']['type'] == 'image/jpeg')
            || ($_FILES['cover_img']['type'] == 'image/jpg')
            || ($_FILES['cover_img']['type'] == 'image/pjpeg')
            || ($_FILES['cover_img']['type'] == 'image/x-png')
            || ($_FILES['cover_img']['type'] == 'image/png'))
            && ($_FILES['cover_img']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['cover_img']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return json_encode($data);
            } else {
                $picname = 'cover_img' . '.' . $extension;
                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/product/';
                $url = './temp/' . $request->u_id . '/product/CoverImg/' . $picname;
                $third_dir = './temp/' . $request->u_id . '/product/CoverImg/';
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!is_dir($third_dir)) {
                    mkdir($third_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }

                move_uploaded_file($_FILES['cover_img']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => $url, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    public function ProductImgs(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'img' => 'required',
            'u_id' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        if (!file_exists('./temp/' . $request->u_id . '/product/imgs')) {
            mkdir('./temp/' . $request->u_id . '/product/imgs/');
        }
        $temp = explode('.', $_FILES['img']['name']);
        $extension = end($temp);
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        if ((($_FILES['img']['type'] == 'image/gif')
            || ($_FILES['img']['type'] == 'image/jpeg')
            || ($_FILES['img']['type'] == 'image/jpg')
            || ($_FILES['img']['type'] == 'image/pjpeg')
            || ($_FILES['img']['type'] == 'image/x-png')
            || ($_FILES['img']['type'] == 'image/png'))
            && ($_FILES['img']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['img']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return json_encode($data);
            } else {
                // $totalimgs1 = glob('./product/imgs/' . "*");
                // $FileNumber1= count($totalimgs1);
                // $totalimgs2 = glob('./temp/product/imgs/' . "*");
                // $FileNumber2= count($totalimgs2);
                // $FileNumber= $FileNumber1+ $FileNumber2;
                // $FileNumber++;
                // $picname=$request->id.'_'.$FileNumber.'.'.$extension;
                $picname = $_FILES['img']['name'];
                // $url = './temp/product/imgs/' . $_FILES['img']['name'];
                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/product/';
                $url = './temp/' . $request->u_id . '/product/imgs/' . $picname;
                $third_dir = './temp/' . $request->u_id . '/product/imgs/';
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!is_dir($third_dir)) {
                    mkdir($third_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }
                move_uploaded_file($_FILES['img']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => $url, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    public function TDCode(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'TDCode' => 'required',
            'u_id' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $temp = explode('.', $_FILES['TDCode']['name']);
        $extension = end($temp);
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        if ((($_FILES['TDCode']['type'] == 'image/gif')
            || ($_FILES['TDCode']['type'] == 'image/jpeg')
            || ($_FILES['TDCode']['type'] == 'image/jpg')
            || ($_FILES['TDCode']['type'] == 'image/pjpeg')
            || ($_FILES['TDCode']['type'] == 'image/x-png')
            || ($_FILES['TDCode']['type'] == 'image/png'))
            && ($_FILES['TDCode']['size'] < 4194304)
            && in_array($extension, $allowedExts)) {
            if ($_FILES['TDCode']['error'] > 0) {
                session()->flash('false', '图片错误!');
                $data = ['success' => false, 'message' => '图片错误'];

                return json_encode($data);
            } else {
                $picname = 'TDcode' . '.' . $extension;
                $url = './temp/TDCode/' . $picname;
                $first_dir = './temp/' . $request->u_id . '/';
                $secend_dir = './temp/' . $request->u_id . '/TDCode/';
                $url = './temp/' . $request->u_id . '/TDCode/' . $picname;
                if (!is_dir($first_dir)) {
                    mkdir($first_dir, 0777);
                }
                if (!is_dir($secend_dir)) {
                    mkdir($secend_dir, 0777);
                }
                if (!file_exists($url)) {
                    $counter_file = $url;
                    $fopen = fopen($counter_file, 'wb'); //新建文件命令
                    fclose($fopen);
                }
                move_uploaded_file($_FILES['TDCode']['tmp_name'], $url);
                $data = ['success' => true, 'message' => '上传成功', 'url' => $url, 'invokeResultCode' => 200];

                return json_encode($data);
            }
        } else {
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];

            return json_encode($data);
        }
    }

    // public function ProductDetailsImg(Request $request){
    //     $validator = Validator::make($request->all(), [
    //         'details_img'=>'required',
    //     ]);
    //     if ($validator->fails()) {
    //         return response()->json(['error' => $validator->errors()], 401);
    //     }
    //     if(!file_exists('./temp/product/DetailsImg'))
    //     {

    //         mkdir('./temp/product/DetailsImg/');
    //     }

    //     $temp = explode('.', $_FILES['details_img']['name']);
    //     $extension = end($temp);
    //     $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
    //     if ((($_FILES['details_img']['type'] == 'image/gif')
    //     || ($_FILES['details_img']['type'] == 'image/jpeg')
    //     || ($_FILES['details_img']['type'] == 'image/jpg')
    //     || ($_FILES['details_img']['type'] == 'image/pjpeg')
    //     || ($_FILES['details_img']['type'] == 'image/x-png')
    //     || ($_FILES['details_img']['type'] == 'image/png'))
    //     && ($_FILES['details_img']['size'] < 4194304)
    //     && in_array($extension, $allowedExts))
    // {
    //     if($_FILES['details_img']['error'] > 0)
    //     {
    //         session()->flash('false', '图片错误!');
    //         $data = ['success' => false, 'message' => '图片错误'];
    //         return  json_encode($data);
    //     }
    //     else
    //     {
    //         $picname='details_img'.'.'.$extension;
    //         $url = './temp/product/DetailsImg/' . $picname;
    //         move_uploaded_file($_FILES['details_img']['tmp_name'], $url);
    //         $data = ['success' => true, 'message' => '上传成功', 'url' => $url,'invokeResultCode' => 200];
    //         return json_encode($data);
    //     }
    // }
    // else{
    //     session()->flash('false', '格式错误!');
    //     $data = ['success' => false, 'message' => '格式错误'];
    //     return  json_encode($data);
    // }
    // }

    // public function ProductConModeImg(Request $request){
    //     $validator = Validator::make($request->all(), [
    //         'ConMode_img'=>'required',
    //     ]);
    //     if ($validator->fails()) {
    //         return response()->json(['error' => $validator->errors()], 401);
    //     }
    //     if(!file_exists('./temp/product/ConModeImg'))
    //     {

    //         mkdir('./temp/product/ConModeImg/');
    //     }

    //     $temp = explode('.', $_FILES['ConMode_img']['name']);
    //     $extension = end($temp);
    //     $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
    //     if ((($_FILES['ConMode_img']['type'] == 'image/gif')
    //     || ($_FILES['ConMode_img']['type'] == 'image/jpeg')
    //     || ($_FILES['ConMode_img']['type'] == 'image/jpg')
    //     || ($_FILES['ConMode_img']['type'] == 'image/pjpeg')
    //     || ($_FILES['ConMode_img']['type'] == 'image/x-png')
    //     || ($_FILES['ConMode_img']['type'] == 'image/png'))
    //     && ($_FILES['ConMode_img']['size'] < 4194304)
    //     && in_array($extension, $allowedExts))
    // {
    //     if($_FILES['ConMode_img']['error'] > 0)
    //     {
    //         session()->flash('false', '图片错误!');
    //         $data = ['success' => false, 'message' => '图片错误'];
    //         return  json_encode($data);
    //     }
    //     else
    //     {
    //         $picname='ConMode_img'.'.'.$extension;
    //         $url = './temp/product/ConModeImg/' . $picname;
    //         move_uploaded_file($_FILES['ConMode_img']['tmp_name'], $url);
    //         $data = ['success' => true, 'message' => '上传成功', 'url' => $url,'invokeResultCode' => 200];
    //         return json_encode($data);
    //     }
    // }
    // else{
    //     session()->flash('false', '格式错误!');
    //     $data = ['success' => false, 'message' => '格式错误'];
    //     return  json_encode($data);
    // }
    // }
}
